package agentV1

import (
	"github.com/gin-gonic/gin"
	"github.com/spf13/cast"
	"hc_server/pkg/app"
	"hc_server/routers/service/agentService"
	"hc_server/routers/service/salesmenService"
	"hc_server/runtime/e"
)

type TeamManagerSalesmenListParam struct {
	AgentId  any `json:"agentId"`
	PageNum  any `json:"pageNum"`
	PageSize any `json:"pageSize"`
}

// TeamManagerSalesmenList 获取销售员列表
func TeamManagerSalesmenList(c *gin.Context) {
	appG := app.Gin{C: c}
	var (
		param   TeamManagerSalesmenListParam
		errCode int
	)

	_, errCode = app.BindJsonAndValid(c, &param)
	if errCode != e.SUCCESS {
		appG.ResponseError(e.CodeMap[errCode], nil)
		return
	}

	salesmenService := salesmenService.TeamMerchantSalesmen{
		Id:       cast.ToInt(param.AgentId),
		PageNum:  cast.ToInt(param.PageNum),
		PageSize: cast.ToInt(param.PageSize),
	}
	result, err := salesmenService.TeamMerchantSalesmenList()
	if err != nil {
		appG.ResponseError(err.Error(), nil)
		return
	}
	appG.ResponseSuccess("成功", result)
}

type TeamManagerAgentListParam struct {
	AgentId  any `json:"agentId"`
	PageNum  any `json:"pageNum"`
	PageSize any `json:"pageSize"`
}

// TeamManagerAgentList 团队管理代理商列表
func TeamManagerAgentList(c *gin.Context) {
	appG := app.Gin{C: c}
	var (
		param   TeamManagerAgentListParam
		errCode int
	)

	_, errCode = app.BindJsonAndValid(c, &param)
	if errCode != e.SUCCESS {
		appG.ResponseError(e.CodeMap[errCode], nil)
		return
	}

	agentService := agentService.AgentTeamManager{
		Id:       cast.ToInt(param.AgentId),
		PageNum:  cast.ToInt(param.PageNum),
		PageSize: cast.ToInt(param.PageSize),
	}
	result, err := agentService.TeamManagerAgentList()
	if err != nil {
		appG.ResponseError(err.Error(), nil)
		return
	}
	appG.ResponseSuccess("成功", result)
}

type TeamManagerAgentInfoParam struct {
	AgentId any `json:"agentId"`
}

// TeamManagerAgentInfo 团队管理代理商详情
func TeamManagerAgentInfo(c *gin.Context) {
	appG := app.Gin{C: c}
	var (
		param   TeamManagerAgentInfoParam
		errCode int
	)

	_, errCode = app.BindJsonAndValid(c, &param)
	if errCode != e.SUCCESS {
		appG.ResponseError(e.CodeMap[errCode], nil)
		return
	}

	agentService := agentService.AgentTeamManager{
		Id: cast.ToInt(param.AgentId),
	}

	result, err := agentService.TeamManagerAgentInfo()
	if err != nil {
		appG.ResponseError(err.Error(), nil)
		return
	}
	appG.ResponseSuccess("成功", result)
}

type TeamManagerAgentRateInfoParam struct {
	AgentId any `json:"agentId"`
}

// TeamManagerAgentRateInfo 团队管理代理商费率
func TeamManagerAgentRateInfo(c *gin.Context) {
	appG := app.Gin{C: c}
	var (
		param   TeamManagerAgentInfoParam
		errCode int
	)

	_, errCode = app.BindJsonAndValid(c, &param)
	if errCode != e.SUCCESS {
		appG.ResponseError(e.CodeMap[errCode], nil)
		return
	}

	agentService := agentService.Rate{
		AgentId: cast.ToInt(param.AgentId),
	}

	result, err := agentService.AgentRateGetRateInfo()
	if err != nil {
		appG.ResponseError(err.Error(), nil)
		return
	}
	appG.ResponseSuccess("成功", result)
}

type TeamManagerAgentRateUpdateParam struct {
	AgentId          any `json:"agentId"`          //代理商id
	RateId           any `json:"rateId"`           //费率id
	Wechat           any `json:"wechat"`           //微信
	Alipay           any `json:"alipay"`           //支付宝
	YunpayLow        any `json:"yunpayLow"`        //云闪付 <= 1000元
	YunpayHigh       any `json:"yunpayHigh"`       //云闪付>1000元
	Debit            any `json:"debit"`            //刷卡储蓄卡
	DebitCapping     any `json:"debitCapping"`     //刷卡储蓄卡封顶手续费
	Credit           any `json:"credit"`           //刷卡信用卡
	SettlementId     any `json:"settlementId"`     //结算id
	D0SettlementRate any `json:"d0SettlementRate"` //D0结算费率
	D0Settlement     any `json:"d0Settlement"`     //D0结算固定手续费
	D1SettlementRate any `json:"d1SettlementRate"` //D1结算费率
	D1Settlement     any `json:"d1Settlement"`     //D1结算固定手续费
	WithdrawalRate   any `json:"withdrawalRate"`   //提现费率
	Withdrawal       any `json:"withdrawal"`       //提现固定手续费
}

// TeamManagerAgentRateUpdate 代理商费率修改
func TeamManagerAgentRateUpdate(c *gin.Context) {
	appG := app.Gin{C: c}
	var (
		param   TeamManagerAgentRateUpdateParam
		errCode int
	)

	_, errCode = app.BindJsonAndValid(c, &param)
	if errCode != e.SUCCESS {
		appG.ResponseError(e.CodeMap[errCode], nil)
		return
	}

	agentService := agentService.Rate{
		AgentId:             cast.ToInt(param.AgentId),
		RateId:              cast.ToInt(param.RateId),
		WechatMin:           cast.ToFloat64(param.Wechat),
		AlipayMin:           cast.ToFloat64(param.Alipay),
		YunPayLowMin:        cast.ToFloat64(param.YunpayLow),
		YunPayHighMin:       cast.ToFloat64(param.YunpayHigh),
		DebitCardMin:        cast.ToFloat64(param.Debit),
		DebitCardCappingMin: cast.ToFloat64(param.DebitCapping),
		CreditCardMin:       cast.ToFloat64(param.Credit),
		SettlementId:        cast.ToInt(param.SettlementId),
		D0SettlementRateMin: cast.ToFloat64(param.D0SettlementRate),
		D0SettlementMin:     cast.ToFloat64(param.D0Settlement),
		D1SettlementRateMin: cast.ToFloat64(param.D1SettlementRate),
		D1SettlementMin:     cast.ToFloat64(param.D1Settlement),
		WithdrawalRateMin:   cast.ToFloat64(param.WithdrawalRate),
		WithdrawalMin:       cast.ToFloat64(param.Withdrawal),
	}

	err := agentService.AgentRateUpdateRateInfo()
	if err != nil {
		appG.ResponseError(err.Error(), nil)
		return
	}
	appG.ResponseSuccess("成功", nil)
}
